<?php

namespace admin\model;

use admin\model\Model;

class UserModel extends Model{

    /**
     * 查询用户的数量
     */
    public function getUserCount($params){
        $sql = 'SELECT COUNT(*) AS count FROM user WHERE is_manager=0 ';
        if(isset($params['name'])){
            $sql.=' AND name LIKE :name';
            $data = $this->p_query($sql,array('name'=>'%'.$params['name'].'%'));
        }else{
            $data = $this->query($sql);
        }
        return $data;
    }
    /**
     * 查询用户所有信息并返回
     */
    public function getUserAll($params = null){
        $curr = ($params['page']-1)*$params['limit'];
        if($params['state'] == 'normal'){
            $sql = 'SELECT id,name,portrait,create_time,last_time,is_blog,is_reply,state,frozen FROM user WHERE is_manager = 0';     //查找正常的
            $data = $this->queryAll($sql);
        }else{
            $sql = 'SELECT id,name,portrait,create_time,last_time,is_blog,is_reply,state,frozen FROM user WHERE is_manager = 0';
            if(isset($params['name'])){
                $sql.=" AND name LIKE :name ORDER BY user.id ASC LIMIT {$curr},{$params['limit']} ";
                $data = $this->p_queryAll($sql,array('name'=>'%'.$params['name'].'%'));
            }else{
                $sql.=" ORDER BY user.id ASC LIMIT {$curr},{$params['limit']} ";
                $data = $this->queryAll($sql);
            }
        } 
        return $this->filter($data);      //过滤数据传给前台
    }

    /**
     * 过滤数据库查找的数据
     */
    public function filter($data){
        $len = count($data,COUNT_NORMAL);       //统计数组长度
        for($i=0;$i<$len;++$i){
            $data[$i]['create_time'] = date("Y-m-d H:i",$data[$i]['create_time']);       //转换账户创建时间
            $data[$i]['last_time'] = date("Y-m-d H:i",$data[$i]['last_time']);       //转换上次登陆时间
            $data[$i]['is_blog'] = $data[$i]['is_blog']==='0'?'不允许':'允许';
            $data[$i]['is_reply'] = $data[$i]['is_reply']==='0'?'不允许':'允许';
            $data[$i]['state'] = $data[$i]['state']==='0'?'离线':'在线';
            $data[$i]['frozen'] = $data[$i]['frozen']==='0'?'冻结':'正常';
        }
        return $data;
    }
    /**
     * 修改用户信息
     */
    public function updateUser($params){
        $sql = 'UPDATE user SET ';
        if(isset($params['is_blog']) && isset($params['is_reply']) && isset($params['name'])){
            $sql.= 'name=:name, is_blog=:is_blog, is_reply=:is_reply, frozen=:frozen ';
        }else{
            $sql.='frozen=:frozen ';
        }
        $sql.='WHERE id = :id';
        $this->p_execute($sql,$params);
        if($this->db->num == 1 ){
            $res['code'] = '1';
        }else{
            $res['code'] = '0';
        }
        return $res;
    }
    /**
     * 删除用户
     */
    public function delUser($params){
        $sql = 'DELETE FROM user WHERE id = :id';
        $this->p_execute($sql,$params);
        if($this->db->num == 1 ){
            $res['code'] = '1';
        }else{
            $res['code'] = '0';
        }
        return $res;
    }
    /**
     * 添加用户
     */
    public function addUser($params){
        $sql = 'INSERT user(user.name,user.password,user.email,user.portrait,user.create_time) 
        VALUES(:name,:password,:email,:portrait,:create_time)';
        $hasher = new PasswordHash(8, false); // 初始化散列器为不可移植(这样更安全)
        $params['password'] = $hasher->HashPassword($params['password'].$params['create_time']);
        $this->p_execute($sql,$params);
        if($this->db->insertId != null){
            $res['code'] = '1';
        }else{
            $res['code'] = '0';
        }
        return $res;
    }
    /**
     * 查找指定名称的用户
     */
    public function getUserName($params){
        $sql = 'SELECT id FROM user WHERE name=:name';
        return $this->p_query($sql,$params);
    }
    /**
     * 查找管理员信息 
     */
    public function getManInfo($params){
        $sql = 'SELECT name,email,portrait,create_time,last_time FROM user WHERE id = :id AND is_manager = 1 ';
        $data = $this->p_query($sql,$params);
        $data['create_time'] = date("Y-m-d H:i",$data['create_time']);       //转换账户创建时间
        $data['last_time'] = date("Y-m-d H:i",$data['last_time']);       //转换上次登陆时间
        return $data;
    }
    /**
     * 修改管理员信息
     */
    public function updateMan($params){
        $sql = 'UPDATE user SET name = :name,email =:email ,portrait=:portrait WHERE is_manager = 1 AND id=:id';
        $this->p_execute($sql,$params);
        if($this->db->num == 1 ){
            $res['code'] = '1';
        }else{
            $res['code'] = '0';
        }
        return $res;
    }
    /**
     * 更新管理员的密码
     */
    public function updateManagerPass($params){
        $data=[];
        $data['code']=0;
        $sql = 'SELECT email,create_time,password FROM user WHERE id = :id AND is_manager = 1 ';
        $man = $this->p_query($sql,array('id'=>$params['id']));
        if($man){
            if($man['email'] == $params['email']){
                $hasher = new PasswordHash(8, false); // 初始化散列器为不可移植(这样更安全)\
                $pass = $params['oldpass'].$man['create_time'];
                if($hasher->CheckPassword($pass, $man['password'])){
                    $hashPassword = $hasher->HashPassword($params['password'].$man['create_time']);     //生成密码
                    $sql2 = 'UPDATE user SET password=:password WHERE id=:id';
                    $this->p_execute($sql2,array('id'=>$params['id'],'password'=>$hashPassword));
                    if($this->db->num == 1){       
                        $data['code'] = '1';         //修改成功
                    }else{
                        $data['code'] = '0';         //修改失败
                    }
                }else{
                    $data['code'] = 2;          //原密码错误
                }
            }else{
                $data['code'] = 3;          //邮箱错误
            }
        }else{
            $data['code'] = 0;          //邮箱错误
        }
        return $data;
    }

}